package cn.iocoder.yudao.module.cmt.controller.admin.venue;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.cmt.controller.admin.venue.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.venue.VenueBookingDO;
import cn.iocoder.yudao.module.cmt.service.venue.VenueBookingService;

@Tag(name = "管理后台 - 阵地预约")
@RestController
@RequestMapping("/cmt/venue-booking")
@Validated
public class VenueBookingController {

    @Resource
    private VenueBookingService venueBookingService;

    @PostMapping("/create")
    @Operation(summary = "创建阵地预约")
    @PreAuthorize("@ss.hasPermission('cmt:venue-booking:create')")
    public CommonResult<Long> createVenueBooking(@Valid @RequestBody VenueBookingSaveReqVO createReqVO) {
        return success(venueBookingService.createVenueBooking(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新阵地预约")
    @PreAuthorize("@ss.hasPermission('cmt:venue-booking:update')")
    public CommonResult<Boolean> updateVenueBooking(@Valid @RequestBody VenueBookingSaveReqVO updateReqVO) {
        venueBookingService.updateVenueBooking(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除阵地预约")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cmt:venue-booking:delete')")
    public CommonResult<Boolean> deleteVenueBooking(@RequestParam("id") Long id) {
        venueBookingService.deleteVenueBooking(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除阵地预约")
    @PreAuthorize("@ss.hasPermission('cmt:venue-booking:delete')")
    public CommonResult<Boolean> deleteVenueBookingList(@RequestParam("ids") List<Long> ids) {
        venueBookingService.deleteVenueBookingListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得阵地预约")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cmt:venue-booking:query')")
    public CommonResult<VenueBookingRespVO> getVenueBooking(@RequestParam("id") Long id) {
        VenueBookingDO venueBooking = venueBookingService.getVenueBooking(id);
        return success(BeanUtils.toBean(venueBooking, VenueBookingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得阵地预约分页")
    @PreAuthorize("@ss.hasPermission('cmt:venue-booking:query')")
    public CommonResult<PageResult<VenueBookingRespVO>> getVenueBookingPage(@Valid VenueBookingPageReqVO pageReqVO) {
        PageResult<VenueBookingDO> pageResult = venueBookingService.getVenueBookingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, VenueBookingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出阵地预约 Excel")
    @PreAuthorize("@ss.hasPermission('cmt:venue-booking:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportVenueBookingExcel(@Valid VenueBookingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<VenueBookingDO> list = venueBookingService.getVenueBookingPage(pageReqVO).getList();
        ExcelUtils.write(response, "阵地预约.xls", "数据", VenueBookingRespVO.class,
                        BeanUtils.toBean(list, VenueBookingRespVO.class));
    }

}
